/* eslint-disable no-unused-vars */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import ClientInfo from '../components/ClientControl/ClientInfo/ClientInfo'
import ClientClass from '../components/ClientControl/ClientClass/ClientClass'
import AuthorInfo from '../components/AuthorControl/AuthorInfo/AuthorInfo'
import AuthorClass from '../components/AuthorControl/AuthorClass/AuthorClass'
import ClientActiveBook from '../components/ClientControl/ClientActive/ClientActiveBook'
import ClientActiveEmail from '../components/ClientControl/ClientActive/ClientActiveEmail'
import ClientActiveGift from '../components/ClientControl/ClientActive/ClientActiveGift'
import AuthorBook from '../components/AuthorControl/AuthorActive/AuthorBook'
import AuthorInvite from '../components/AuthorControl/AuthorActive/AuthorInvite'
import AuthorTalk from '../components/AuthorControl/AuthorActive/AuthorTalk'




Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login,
        meta: {
            title: '登录'
        }
    },
    {
        path: '/home',
        component: Home,
        redirect: '/home/welcome',
        meta: {
            title: '主页'
        },
        children: [{
                path: 'welcome',
                component: Welcome,
                meta: {
                    title: '主页'
                },
            },
            {
                path: 'clientInfo',
                component: ClientInfo,
                meta: {
                    title: '客户信息'
                },
            },
            {
                path: 'clientClass',
                component: ClientClass,
                meta: {
                    title: '客户类别'
                },
            },
            {
                path: 'authorInfo',
                component: AuthorInfo,
                meta: {
                    title: '作者信息'
                },
            },
            {
                path: 'authorClass',
                component: AuthorClass,
                meta: {
                    title: '作者类别'
                },
            },
            {
                path: 'clientActiveBook',
                component: ClientActiveBook,
                meta: {
                    title: '客户活动'
                },
            },
            {
                path: 'clientActiveEmail',
                component: ClientActiveEmail,
                meta: {
                    title: '客户活动'
                },
            },
            {
                path: 'clientActiveGift',
                component: ClientActiveGift,
                meta: {
                    title: '客户活动'
                },
            },
            {
                path: 'authorBook',
                component: AuthorBook,
                meta: {
                    title: '作者事务'
                },
            },
            {
                path: 'authorInvite',
                component: AuthorInvite,
                meta: {
                    title: '作者事务'
                },
            },
            {
                path: 'authorTalk',
                component: AuthorTalk,
                meta: {
                    title: '作者事务'
                },
            }
        ]
    }
]

const router = new VueRouter({
    routes
})


//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    if (to.path == '/home/welcome') sessionStorage.removeItem('activePath')
    if (to.path == '/login') return next()
    const tokenStr = sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

router.afterEach((to, from) => {
    document.title = to.meta.title
})

export default router